var cluster = require('cluster');
var http = require('http');
// 确定服务器的内核数
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
	// 每个内核创建一个fork
	for (var i = 0; i < numCPUs; i++) {
		cluster.fork();
	}

	// 监听子进程的退出消息
	cluster.on('exit', function(worker, code, signal) {
		console.log('Worker ' + worker.process.pid + ' died.');
	});
} else {
	http.createServer(function(req, res) {
		res.writeHead(200);
		res.end('I am a worker running in process ' + process.pid);
	}).listen(8000);
}